// 登录与注册的仓库
import { reqCode, reqUserRegister, reqUserLogin, reqUserInfo } from '../../api'
const actions = {
    // 获取验证码
    async getCode(context, phone) {
        let result = await reqCode(phone)
        if (result.code == 200) {
            context.commit("GETCODE", result.data)
            return 'OK'
        } else {
            return Promise.reject(new Error('faile'))
        }
    },
    // 用户注册
    async userRegister(context, user) {
        let result = await reqUserRegister(user);
        if (result.code == 200) {
            return 'OK'
        } else {
            return Promise.reject(new Error('faile'))
        }
    },
    // 用户登录
    async userLogin(context, user) {
        let result = await reqUserLogin(user);
        console.log(result);
        context.commit('USERLOGIN', result.data.token);
    },
    // 获取用户的信息
    async userInfo(context) {
        let result = await reqUserInfo();
        if (result.code == 200) {
            context.commit('USERINFO', result.data)
        }
    }
}

const mutations = {
    GETCODE(state, code) {
        state.code = code
    },
    USERLOGIN(state, token) {
        state.token = token;
    },
    USERINFO(state, data) {
        state.userInfo = data
    }
}

const state = {
    code: '',
    token: '',
    userInfo: {}
}

const getters = {

}

export default {
    actions,
    mutations,
    state,
    getters
}